package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyCatalog;
import cn.wolfcode.trip.base.mapper.StrategyCatalogMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StrategyCatalogServiceImpl implements IStrategyCatalogService {
    @Autowired
    private StrategyCatalogMapper strategyCatalogMapper;

    public PageInfo<?> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        return new PageInfo<>(strategyCatalogMapper.selectForList(qo));
    }

    public void saveOrUpdate(StrategyCatalog entity) {
        if (entity.getSequence()==null){
            Integer sequence = strategyCatalogMapper.getMax(entity.getStrategy().getId());
            if (sequence==null) {
                entity.setSequence(1);
            }else {
                entity.setSequence(sequence+1);
            }
        }
        if (entity.getId()!=null){
            strategyCatalogMapper.updateByPrimaryKey(entity);
        }else {
            strategyCatalogMapper.insert(entity);
        }
    }

    @Override
    public List<StrategyCatalog> listAll() {
        return strategyCatalogMapper.selectAll();
    }

    public List<StrategyCatalog> listByStrategyId(Long strategyId) {
        return strategyCatalogMapper.selectByStrategyId(strategyId);
    }

}
